<template>
  <el-container class="home-container">
    <el-header>
      <div class="left">
        <img src="../../assets/logo.png" alt />
        <span>主观题智能阅卷系统</span>
      </div>
      <div class="right">
        <el-avatar icon="el-icon-user-solid"></el-avatar>

        <el-dropdown @command="nav">
          <span class="el-dropdown-link">
            开发者
            <i class="el-icon-arrow-down el-icon--right"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <!-- 好像不能直接写@click -->
            <el-dropdown-item command="/adminhome">系统管理</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>

        <el-button icon="el-icon-switch-button" @click="logout()">退出</el-button>
      </div>
    </el-header>

    <el-container>
      <el-main>
        <div>
          <span>欢迎来到开发者模块</span>
          <el-divider></el-divider>
        </div>

        <!-- 卡片视图 -->
        <el-card>
          <!-- 作业列表 -->
          <!-- border加边框 -->
          <!-- stripe隔行变色 -->

          <el-table :data="homeworklist" style="width: 100%">
            <el-table-column label="作业ID" prop="id"></el-table-column>
            <el-table-column label="出题者" prop="teacherName"></el-table-column>
            <el-table-column label="答题者" prop="userName"></el-table-column>
            <el-table-column label="答题时间" prop="answerTime"></el-table-column>
            <el-table-column label="得分" prop="score" align="center"></el-table-column>
            <el-table-column align="right">
              <!-- 与后端联调时需要template, 可能 -->
              <template slot-scope="scope">
                <el-button type="success" @click="navdetail(scope.$index)" size="small">查看</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      homeworklist: []
    }
  },
  created() {
    this.getMenuList()
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$store.commit('clear')
      this.$router.push('/login')
    },
    // 获取所有的菜单，从后端
    async getMenuList() {
      // 取出的data重命名为res
      const { data: res } = await this.$http.get('user/answer/getSimpleAnswer')
      if (res.code !== 200) return this.$message.error(res.message)
      this.homeworklist = res.data

      // 时间处理，但其实最好是后端自己处理好
      for (var i = 0; i < this.homeworklist.length; i++) {
        // 结束时间
        this.homeworklist[i].answerTime = /\d{4}-\d{1,2}-\d{1,2}/g.exec(
          this.homeworklist[i].answerTime
        )[0]
      }
      console.log(res)
    },

    // 跳转页面
    nav(command) {
      this.$router.push(command)
    },

    //
    navdetail(index) {
      // 之后加上id，动态路由跳转
      this.$router.push('/admin/devdetail/' + this.homeworklist[index].id)
    }
  }
}
</script>

<style lang="less" scoped>
.el-menu {
  .el-menu-item {
    text-align: center;
  }
  .el-menu-item.is-active {
    background: #1aad19 !important;
  }
}

.sty {
  // font-weight: bold;
  font-size: 20px;
}

.home-container {
  height: 100%;
  width: 80%;
  margin-left: 10%;
  background: #f0f0f2;
}

.el-container {
  margin-top: 15px;
}

.el-header {
  // background-color: #383f86;
  background-color: white;
  // margin-left: 10%;
  // width: 80%;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: black;
  // color: #ad9fbd;
  font-size: 20px;
  .left {
    display: flex;
    align-items: center;
    margin-left: 10px;
    img {
      width: 5%;
      height: 5%;
    }
    span {
      margin-left: 15px;
    }
  }
  .right {
    display: flex;
    align-items: center;
    .el-dropdown {
      span {
        font-size: 15px;
        white-space: nowrap;
      }
    }
  }
}

.el-button {
  margin-left: 50px;
  font-size: 15px;
  // background: white;
  border: none;
}
.el-avatar {
  margin-right: 50px;
}

.el-aside {
  background-color: #f0f0f2;
  height: auto;
  margin-left: 15px;
  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: white;
  margin-left: 15px;
  margin-right: 15px;

  // background-image: url("../assets/微信图片_20191012142541.jpg");
  // background-size: 1336px 694.4px;
}

.iconfont {
  margin-right: 10px;
}

.toggle-button {
  background-color: white;
  font-size: 10px;
  line-height: 24px;
  color: black;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>